import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

import java.io.IOException;
import java.util.ArrayList;

public class EDIT extends Contronller{
    public static void Edit(){ AnchorPane pane = new AnchorPane();
        Scene scene = new Scene(pane);
        Stage stage = new Stage();
        stage.setScene(scene);
        stage.setTitle("Edit Staff");
        pane.setPrefSize(400, 300);
        Label label = new Label("请输入所编辑的员工姓名");
        TextField textField = new TextField();
        pane.getChildren().add(label);
        pane.getChildren().add(textField);
        AnchorPane.setTopAnchor(label, 75.0);
        AnchorPane.setTopAnchor(textField, 75.0);
        AnchorPane.setLeftAnchor(label, 25.0);
        AnchorPane.setLeftAnchor(textField, 175.0);
        Button button1 = new Button("返回");
        Button button2 = new Button("确定");
        pane.getChildren().add(button1);
        pane.getChildren().add(button2);
        AnchorPane.setTopAnchor(button1, 225.0);
        AnchorPane.setTopAnchor(button2, 225.0);
        AnchorPane.setLeftAnchor(button1, 45.0);
        AnchorPane.setLeftAnchor(button2, 225.0);
        button1.setOnAction(e -> {stage.close();});
        button2.setOnAction(e -> {
            Main.staffList.forEach(staff -> {
                int index = Main.staffList.indexOf(staff);
                if (textField.getText().equals(staff.getName())) {
                    stage.close();
                    GridPane gridPane = new GridPane();
                    Scene scene2 = new Scene(gridPane);
                    Stage stage2 = new Stage();
                    stage2.setScene(scene2);
                    stage2.setTitle(staff.getName());
                    gridPane.setPrefSize(900, 450);
                    gridPane.setVgap(15);
                    gridPane.setHgap(10);
                    Label label1 = new Label("姓名");
                    Label label2 = new Label("性别");
                    Label label3 = new Label("年龄");
                    Label label4 = new Label("入职时间");
                    Label label5 = new Label("住址");
                    Label label6 = new Label("电话");
                    Label label7 = new Label("工资");
                    Label label8 = new Label("职位");
                    label1.setPrefSize(80, 30);
                    label2.setPrefSize(80, 30);
                    label3.setPrefSize(80, 30);
                    label4.setPrefSize(80, 30);
                    label5.setPrefSize(80, 30);
                    label6.setPrefSize(80, 30);
                    label7.setPrefSize(80, 30);
                    label8.setPrefSize(80, 30);
                    gridPane.add(label1, 1, 1);
                    gridPane.add(label2, 1, 3);
                    gridPane.add(label3, 1, 5);
                    gridPane.add(label4, 1, 7);
                    gridPane.add(label5, 1, 9);
                    gridPane.add(label6, 1, 11);
                    gridPane.add(label7, 1, 13);
                    gridPane.add(label8, 1, 15);
                    TextArea textArea1 = new TextArea();
                    textArea1.setEditable(true);
                    TextArea textArea2 = new TextArea();
                    textArea2.setEditable(true);
                    TextArea textArea3 = new TextArea();
                    textArea3.setEditable(true);
                    TextArea textArea4 = new TextArea();
                    textArea4.setEditable(true);
                    TextArea textArea5 = new TextArea();
                    textArea5.setEditable(true);
                    TextArea textArea6 = new TextArea();
                    textArea6.setEditable(true);
                    TextArea textArea7 = new TextArea();
                    textArea7.setEditable(true);
                    TextArea textArea8 = new TextArea();
                    textArea8.setEditable(true);
                    textArea1.setText(staff.getName());
                    textArea2.setText(staff.getGender());
                    textArea3.setText(staff.getAge());
                    textArea4.setText(staff.getTime());
                    textArea5.setText(staff.getAddress());
                    textArea6.setText(staff.getPhonenum());
                    textArea7.setText(staff.getSalary());
                    textArea8.setText(staff.getPosition());
                    gridPane.add(textArea1, 3, 1);
                    gridPane.add(textArea2, 3, 3);
                    gridPane.add(textArea3, 3, 5);
                    gridPane.add(textArea4, 3, 7);
                    gridPane.add(textArea5, 3, 9);
                    gridPane.add(textArea6, 3, 11);
                    gridPane.add(textArea7, 3, 13);
                    gridPane.add(textArea8, 3, 15);
                    Button button = new Button("确定");
                    button.setPrefSize(80, 30);
                    gridPane.add(button, 1, 29);
                    button.setOnAction(actionEvent -> {stage2.close();});
                    Button saveButton = new Button("保存");
                    saveButton.setPrefSize(80, 30);
                    gridPane.add(saveButton, 19, 29);
                    saveButton.setOnAction(actionEvent -> {
                        staff.setName(textArea1.getText());
                        staff.setGender(textArea2.getText());
                        staff.setAge(textArea3.getText());
                        staff.setTime(textArea4.getText());
                        staff.setAddress(textArea5.getText());
                        staff.setPhonenum(textArea6.getText());
                        staff.setSalary(textArea7.getText());
                        staff.setPosition(textArea8.getText());
                    });
                    stage2.show();
                }
                if (stage.isShowing()&&index == Main.staffList.size() - 1&&!(textField.getText().equals(staff.getName()))) {
                    stage.close();
                    GridPane gridPane = new GridPane();
                    Scene scene2 = new Scene(gridPane);
                    Stage stage2 = new Stage();
                    stage2.setScene(scene2);
                    stage2.setTitle("错误");
                    gridPane.setPrefSize(300, 150);
                    gridPane.setVgap(15);
                    gridPane.setHgap(10);
                    Button button = new Button("确定");
                    gridPane.add(button, 10, 10);
                    button.setOnAction(actionEvent -> {stage2.close();});
                    stage2.show();
                }
            });
        });
        stage.show();
    }
}
